/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.common;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.EntityGeneratorContext;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CastExpression;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;

public class DataEqualsMethodGenerator extends ClassMethodGenerator {

  private final EntityGeneratorContext ctx;
  private final String nodeCode;
  private List<MediatePropertyInfo> propertyInfos;
  private String className;
  private String dataImplClassName;
  private String targetInterfaceType;

  //
//  public DataEqualsMethodGenerator(List<MediatePropertyInfo> propertyInfos,String className)
//  {
//    this.propertyInfos = propertyInfos;
//    this.className = className;
//    this.targetInterfaceType = null;
//  }
  public DataEqualsMethodGenerator(List<MediatePropertyInfo> propertyInfos,String className,String nodeCode,
      String targetInterfaceType, EntityGeneratorContext ctx) {
    this.nodeCode = nodeCode;
    this.propertyInfos = propertyInfos;
    this.className = className;
    this.targetInterfaceType = targetInterfaceType;
    this.ctx = ctx;
  }

  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();
    addCheckNullExpression(block);
    addCastExpression(block);
    addInvokeEqualsExpression(block);
//    addCheckPropertiesExpression(block);
//    addReturnTrueExpression(block);
    return block;
  }

  private void addInvokeEqualsExpression(Block block) {
    ReturnStatement returnStatement=ast.newReturnStatement();
    MethodInvocation methodInvocation=ast.newMethodInvocation();
    methodInvocation.setExpression(ast.newSimpleName(ctx.getDataImplTypes().get(nodeCode).getFullName()));
    methodInvocation.setName(ast.newSimpleName("equals"));
    methodInvocation.arguments().add(ast.newThisExpression());
    methodInvocation.arguments().add(ast.newSimpleName("newObj"));

    returnStatement.setExpression(methodInvocation);


    block.statements().add(returnStatement);
  }

  private void addReturnTrueExpression(Block block) {
    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ast.newBooleanLiteral(true));
    block.statements().add(returnStatement);
  }

  private void addCheckPropertiesExpression(Block block) {
    for (MediatePropertyInfo propertyInfo : propertyInfos) {
      boolean isChildObj = false;
      if (propertyInfo.getNode() != null) {
        continue;
      }
      addCheckPropExpression(block, propertyInfo, isChildObj);
    }
  }

  private void addCheckPropExpression(Block block, MediatePropertyInfo propertyInfo,
      boolean isChildObj) {
    IfStatement ifStatement = ast.newIfStatement();
    addPropNullStatements(block, propertyInfo, isChildObj, ifStatement);
    addPropEqualsExpression(block, propertyInfo, isChildObj, ifStatement);
    block.statements().add(ifStatement);
  }

  private void addPropEqualsExpression(Block block, MediatePropertyInfo propertyInfo,
      boolean isChildObj, IfStatement ifStatement) {
    IfStatement ifStatement1 = ast.newIfStatement();
    addIfPropEqualsExpression(ifStatement1, propertyInfo, isChildObj);
    addIfPropEqualsThen(ifStatement1, propertyInfo, isChildObj);

    ifStatement.setElseStatement(ifStatement1);
  }

  private void addIfPropEqualsExpression(IfStatement ifStatement, MediatePropertyInfo propertyInfo,
      boolean isChildObj) {
    InfixExpression infixExpression = ast.newInfixExpression();
    addIfPropEqExpressLeft(infixExpression, propertyInfo);
    infixExpression.setRightOperand(ast.newBooleanLiteral(false));
    infixExpression.setOperator(InfixExpression.Operator.EQUALS);
    ifStatement.setExpression(infixExpression);
  }

  private void addIfPropEqExpressLeft(InfixExpression infixExpression,
      MediatePropertyInfo propertyInfo) {
    MethodInvocation methodInvocation = ast.newMethodInvocation();
    addPropEqualsMethodExpression(methodInvocation, propertyInfo);
    addPropEqualsArgs(methodInvocation, propertyInfo);
    methodInvocation.setName(ast.newSimpleName("equals"));
    infixExpression.setLeftOperand(methodInvocation);
  }

  private void addPropEqualsMethodExpression(MethodInvocation methodInvocation,
      MediatePropertyInfo propertyInfo) {
    MethodInvocation methodInvocationGetProp = ast.newMethodInvocation();
    methodInvocationGetProp.setExpression(ast.newThisExpression());
    methodInvocationGetProp.setName(ast.newSimpleName(propertyInfo.getGetterMethodName()));
    methodInvocation.setExpression(methodInvocationGetProp);
  }

  private void addPropEqualsArgs(MethodInvocation methodInvocation,
      MediatePropertyInfo propertyInfo) {
    MethodInvocation methodInvocationGetProp = ast.newMethodInvocation();
    methodInvocationGetProp.setExpression(ast.newSimpleName("newObj"));
    methodInvocationGetProp.setName(ast.newSimpleName(propertyInfo.getGetterMethodName()));
    methodInvocation.arguments().add(methodInvocationGetProp);
  }

  private void addIfPropEqualsThen(IfStatement ifStatement, MediatePropertyInfo propertyInfo,
      boolean isChildObj) {
    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ast.newBooleanLiteral(false));

    ifStatement.setThenStatement(returnStatement);
  }

  private void addPropNullStatements(Block block, MediatePropertyInfo propertyInfo,
      boolean isChildObj, IfStatement ifStatement) {

    addIfPropNullExpression(ifStatement, block, propertyInfo);
    addIfTargetNotNullStatements(ifStatement, block, propertyInfo);

  }

  private void addIfPropNullExpression(IfStatement ifStatement, Block block,
      MediatePropertyInfo propertyInfo) {
    InfixExpression infixExpression = ast.newInfixExpression();
    infixExpression.setOperator(InfixExpression.Operator.EQUALS);

    MethodInvocation methodInvocation = ast.newMethodInvocation();
    methodInvocation.setExpression(ast.newThisExpression());
    methodInvocation.setName(ast.newSimpleName(propertyInfo.getGetterMethodName()));
    infixExpression.setLeftOperand(methodInvocation);
    infixExpression.setRightOperand(ast.newNullLiteral());
    infixExpression.setOperator(InfixExpression.Operator.EQUALS);
    ifStatement.setExpression(infixExpression);
  }

  private void addIfTargetNotNullStatements(IfStatement ifStatement, Block block,
      MediatePropertyInfo propertyInfo) {
    Block block1 = ast.newBlock();
    IfStatement ifStatement1 = ast.newIfStatement();
    InfixExpression infixExpression = ast.newInfixExpression();
    infixExpression.setOperator(InfixExpression.Operator.NOT_EQUALS);

    MethodInvocation methodInvocation = ast.newMethodInvocation();
    methodInvocation.setExpression(ast.newSimpleName("newObj"));
    methodInvocation.setName(ast.newSimpleName(propertyInfo.getGetterMethodName()));
    infixExpression.setLeftOperand(methodInvocation);
    infixExpression.setRightOperand(ast.newNullLiteral());
    ifStatement1.setExpression(infixExpression);

    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ast.newBooleanLiteral(false));
    ifStatement1.setThenStatement(returnStatement);
    block1.statements().add(ifStatement1);
    ifStatement.setThenStatement(block1);
  }

  private void addCastExpression(Block block) {
    VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
    variableDeclarationFragment.setName(ast.newSimpleName("newObj"));
    CastExpression castExpression = ast.newCastExpression();
    if (targetInterfaceType == null || targetInterfaceType.length() == 0) {
      castExpression.setType(ast.newSimpleType(ast.newName(className)));
    } else {
      castExpression.setType(ast.newSimpleType(ast.newName(targetInterfaceType)));
    }
    castExpression.setExpression(ast.newSimpleName("obj"));
    variableDeclarationFragment.setInitializer(castExpression);
    VariableDeclarationStatement variableDeclarationStatement = ast
        .newVariableDeclarationStatement(variableDeclarationFragment);
    if (targetInterfaceType == null || targetInterfaceType.length() == 0) {
      variableDeclarationStatement.setType(ast.newSimpleType(ast.newName(className)));
    } else {
      variableDeclarationStatement.setType(ast.newSimpleType(ast.newName(targetInterfaceType)));
    }
    block.statements().add(variableDeclarationStatement);
  }

  private void addCheckNullExpression(Block block) {
    IfStatement ifStatement = ast.newIfStatement();
    InfixExpression infixExpression = ast.newInfixExpression();
    infixExpression.setOperator(InfixExpression.Operator.EQUALS);
    infixExpression.setLeftOperand(ast.newSimpleName("obj"));
    infixExpression.setRightOperand(ast.newNullLiteral());
    ifStatement.setExpression(infixExpression);

    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ast.newBooleanLiteral(false));
    ifStatement.setThenStatement(returnStatement);
    block.statements().add(ifStatement);
  }

  @Override
  protected boolean getIsOverride() {
    return true;
  }

  @Override
  protected String getMethodName() {
    return "equals";
  }

  @Override
  protected TypeInfo getReturnType() {
    return new TypeInfo("boolean");
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
    return new ArrayList<>(
        Arrays.asList(new Modifier.ModifierKeyword[]{Modifier.ModifierKeyword.PUBLIC_KEYWORD}));
  }

  @Override
  protected ArrayList<ParameterInfo> getParameterCollection() {
    ParameterInfo parameterInfo = new ParameterInfo();
    parameterInfo.setParamName("obj");
    parameterInfo.setParamType(new TypeInfo(Object.class));
    return new ArrayList<>(Arrays.asList(new ParameterInfo[]{parameterInfo}));
  }
}
